// app/model/registration.js
// 挂号预约表，存储挂号的相应信息
'use strict';

module.exports = app => {
  const {
    STRING,
    INTEGER,
    DATE,
  } = app.Sequelize;

  const Registration = app.model.define('registration', {
    // 挂号ID 
    id_registration: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    // 就诊人ID
    id_patient: {
      type: INTEGER,
      primaryKey: true,
    },
    id_case: INTEGER,
    // 挂号时间
    time_registration: {
      type: DATE,
    },
    id_user: STRING,//医生ID
    state_registration: STRING,//挂号就诊状态
  }, {
    freezeTableName: true,
    timestamps: false,
  });

  //挂号关联
  Registration.associate = function () {
    app.model.Registration.hasOne(app.model.Patient, {
      through: Registration,
      foreignKey: 'id_patient',
      otherKey: 'id_patient',
      sourceKey: 'id_patient',
    });
    app.model.Patient.hasOne(app.model.Registration, {
      through: Registration,
      foreignKey: 'id_patient',
      otherKey: 'id_patient',
    });
    app.model.Registration.hasOne(app.model.Case, {
      through: Registration,
      foreignKey: 'id_case',
      otherKey: 'id_case',
      sourceKey: 'id_case',
    });
    app.model.Case.hasOne(app.model.Registration, {
      through: Registration,
      foreignKey: 'id_case',
      sourceKey: 'id_case',
      otherKey: 'id_case',
    });
  };


  return Registration;
};

